var
    gulp = require('gulp'),                             //本地安装gulp所用到的地方
    less = require('gulp-less'),                        //less编译
    autoprefixer = require('gulp-autoprefixer'),        //自动前缀
    minify = require('gulp-clean-css'),                 //css压缩
    uglify = require('gulp-uglify'),                    //js压缩
    concat = require('gulp-concat'),                    //合并
    notify = require('gulp-notify'),                    //编译提示
    watch = require('gulp-watch'),                      //变动监听
    rename = require('gulp-rename'),                    //更改名称
    rev = require('gulp-rev'),                          //生成MD5 版本manifest
    gulpIf = require('gulp-if'),
    //sourcemaps = require('gulp-sourcemaps'),            //
    //plumber = require('gulp-plumber'),                  //避免因错误而中断 gulp
    revReplace = require('gulp-rev-replace'),           //替换引用路径
    runSequence  = require('run-sequence'),             //独立任务
    useref = require('gulp-useref'),                    //html引用文件合并
    cheerio = require('gulp-cheerio');

//路径
var path = {
    watchName:'',
    src:'src/',
    out:'dist/',
    html:'views/',
    source:'/demo/src',
    domain:'/demo/dist/',
    concatCss:[],
    concatJs:[],
    moveAssets:[
        'src/component/**/*',
        'src/css/**/*',
        'src/js/**/*',
        'src/lib/**/*'
    ],
    less:'src/less/',
    css:'src/css/',
    component:'src/component/'
};

//合并css文件并移动至目标文件夹
gulp.task('concatCss',function(){
    gulp.src(path.concatCss)
        .pipe(concat('all.css'))
        .pipe(minify())
        .pipe(gulp.dest(path.out + path.src + 'css'));
});
//合并css文件移动至目标文件夹
gulp.task('concatJs',function(){
    gulp.src(path.concatJs)
        .pipe(concat('all.js'))
        .pipe(uglify())
        .pipe(gulp.dest(path.out + path.src + 'js'));
});
//移动文件至目标文件夹
gulp.task('move',function(){
    gulp.src(path.moveAssets,{base:'.'})
        .pipe(gulpIf('*.css', minify()))
        .pipe(gulpIf('*.js', uglify()))
        .pipe(gulp.dest(path.out))
});



gulp.task('demo',function(){
    gulp.src('views/*.html')
        //不输出合并后的文件
        .pipe(useref({noAssets: true}))
        .pipe(gulp.dest(path.out))
});

//生成版本文件
gulp.task('rev', function () {
    return gulp.src([path.out+'**/*.css',path.out + '**/*.js'])
        .pipe(rev())
        .pipe( rev.manifest() )
        .pipe(rename('rev.json'))
        .pipe( gulp.dest( '/' ) );
});

//添加版本号
gulp.task('rev-add', function() {
    var manifest = gulp.src('/rev.json');
    var out = path.out+path.html;
    function modifyUnreved(filename) {
        return filename;
    }
    function modifyReved(filename) {
        var res = '';
        const version = filename.match(/-([a-z\d]+)\./)[1];
        const file = filename.replace(/-[a-z\d]+\./,'.');
        res = file + "?v=" + version;
        return res;
    }
    gulp.src([out + '**/*.html'])
        .pipe(revReplace({
            manifest: manifest,
            modifyUnreved: modifyUnreved,
            modifyReved: modifyReved
        }))
        .pipe(gulp.dest(out));
});

gulp.task('build-rev',function(callback){
    runSequence('rev','rev-add');
});



/*less 编译*/
gulp.task('style',function(){
    gulp.watch(path.less + '**/*.less',function(event){
        var str = event.path;
        str = str.match(/\\([a-zA-Z\d-]+)\.less$/)[1];
        console.log(str);
        var watchName = str.replace(/\\/,'/');
        return  gulp.src(path.less + '**/' + watchName +'.less')
            .pipe(less())
            .pipe(autoprefixer())
            //.pipe(cssmin())
            .pipe(gulp.dest(path.css))
            .pipe(notify({ message: 'Css编译完成' }));
        //gulp.start('data-style');
    });
});

//编译文件夹所有样式
gulp.task('style-all', function () {
    return  gulp.src(path.less + "**/*.less")
        .pipe(less())
        .pipe(autoprefixer())
        .pipe(minify({}))
        .pipe(gulp.dest(path.css))
        .pipe(notify({ message: 'Css编译完成' }));
});